MathPad may be used as a calculator simply by typing in the numbers and operators. When the ENTER key is hit, each line is evaluated and the results are inserted into the text. Variables and functions can be defined. Results can be plotted or tabulated. Command period can be used to stop evaluation.
~
----------------------- Examples:
21*(3+4)/2.4:61.2
2 + -- expressions are continued if the line ends with an operator
3*4:14.0
#+5:19.0; -- # may be used to access the previous result
------- variables and functions
-- calculate a trajectory for initial velocity V, angle Theta.
Vx=V*cos(Theta)
Vy=V*sin(Theta) when Theta > 0 and Theta < 180, 0 otherwise
altitude(t)=Vy*t-(g/2)*t^2
impact_time=2*(Vy/g)
apogee=(Vy^2)/(2*g); range=Vx*impact_time
g=9.8; V=100; Theta=75
range:510.2; apogee:476.0
------- plotting
-- Special variables used to control plot:
-- X Xmin Xmax Xsteps Xdiv Xlabel
-- Ymin Ymax Ydiv Ylabel Title Ystrips
-- "newaxis" allows multiple plots/page
plot altitude(X)
Xmin=0; Xmax=impact_time
label apogee:476.0
------- tables
-- Special variables used to control table:
-- N Nmin Nmax Nsteps
table N, N^3
1.0 1.0
2.0 8.0
3.0 27.0
4.0 64.0
5.0 125.0
6.0 216.0
7.0 343.0
8.0 512.0
9.0 729.0
10.0 1000.0
------- summation
series(n) = sum(2*k-1,k,1,n)
series(9):81.0
------- recursion
fact(n) = 1 when n ≤ 0, fact(n-1)*n
fact(50):3.0e+64
------- arrays
A={10,20,30} -- Defines a 3 element array.
A[2]:20.0; -- Accesses the 2nd element. Index values start at 1.
B[1,2]:20.0; -- Both forms of indexing are allowed
A[2:3]:{20.0,30.0}; -- [lo:hi] selects a sub array.
A[:2]:{10.0,20.0}; -- lo and/or hi may be omitted.
Q[i]=i*11 --Arrays can be defined in terms of their index values.
Q[2]:22.0
-- dim[] can be used to set the number of elements
I[i,j] = 1 when i=j, 0 dim[3,3]
I:{{1.0,0.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0}}
--Arrays may be used freely in expressions. Operations are performed on each element.
A+Q:{21.0,42.0,63.0}
2+A:{12.0,22.0,32.0}
log(A):{1.0,1.3,1.5}
A*Q:{110.0,440.0,990.0}; --Note: this is not matrix multiply
--Q[A]:? A[?]; --Arrays may not be used as index values
--Functions can use index values
skip(zz,by)[i] = zz[i*by] dim[count(zz)/by]
skip(A,3):{30.0}
skip(Q,2):{22.0,44.0,66.0,...}
multiply(A,B)[i,j] = sum(A[i,k]*B[k,j],k,1,count(B)) dim
[count(A),count(B[1])]
multiply({A},I):{{10.0,20.0,30.0}}
--Built-in functions for arrays
count(A):3.0; -- count() returns the number of array elements
count(B):3.0; -- elements in 1st dimension
count(42):0.0; -- scalar
count(Q):?; -- infinite array
det(B):580.0; -- calculates the determinant of a square matrix
--read("pathname") returns an array of values read from the named file.
--write("pathname",array) writes the elements of the array to the named file.
--------- The table command can print 1D or 2D arrays
table B
10.0 20.0 30.0
40.0 50.0 60.0
11.0 21.0 31.0
--------- The plot command can plot 1D or 2D arrays
--To connect the points, use "plotline" instead of "plot".
--For 1D {y1,y2,y3...} the element values are used for y. X is stepped from Xmin to Xmax as usual.
-- 2D arrays can be given in 3 forms:
-- array of x,y pairs {{x1,y1},{x2,y2},{x3,y3},...}
-- a pair of arrays {{x1,x2,...},{y1,y2,...}}
-- a pair of functions {fx(X),fy(X)}
-- For 2D arrays both x and y are taken from the array. X is stepped from 0 to 1.0 and can be used for parametric equations:
deg=X*360; Xsteps=36 -- run deg from 0 to 360 in 10° steps
x(angle)=5*cos(angle)+10 -- arbitrary scale to fit on existing plot
y(angle)=100*sin(angle)+200
plot {x(deg),y(deg)}
--------- The image command can display 2D arrays
img[i,j]=(i-10)^2-5*j^2 dim[20,20]
image img
Zmax=95; Zmin=-500
--------- assignments
-- The assignment operator := evaluates the right hand side expression and replaces any previous definition of the left hand variable with the result. Accessing the variable gives its current value.
-- Assignments can only be done to simple global variables.
-- Finite arrays may be assigned. A single numeric value can be assigned to an element of an array that has previously been initialized by assignment.
-- The result of the := operation itself is always unknown. If you wish to access the result you must use the variable name. Multiple assignments can be performed using comma separated lists.
--------- iteration
-- The 'while' operator can be used to evaluate some expression repeatedly.
factorial(n) = i:=1,m:=1,(i:=i+1,m:=m*i) while i<n, m
factorial(400):6.4e+868
--------- include files
-- include "pathname" will open and read definitions from the specified file.